﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BusinessObjects.BO_GetObjects;
using BussinessObjects;
using BusinessObjects;
using LibraryO2S;
using BusinessObjects.BO_Actions.Articles;
using System.Collections.Generic;
using Entity.StoreProcedure;

public partial class controls_admin_News : Pages_Base
{
    protected int mid;
    private DataTable objDT;
    private int cid;
    private int mc;
    private string keyword;
    protected void Page_Load(object sender, EventArgs e)
    {
        mid = base.getRequest("mid", -1);
        if (mid > 0)
            ddlCategory.Visible = false;
        cid = base.getRequest("cid", -1);
        keyword = base.getRequest("k_w", string.Empty);
        base.removeHGC(divMessage);
        base.removeHGC(divResult);
        if (!IsPostBack)
        {
            try
            {
                base.loadCategories(ddlCategory, 1);
                if (cid > 0 && mid < 0)
                    ddlCategory.SelectedValue = cid.ToString();
                loadData();
            }
            catch (Exception ex)
            {
                divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Có lỗi khi truy cập hệ thống! <br>" + ex.Message);
            }
            finally { base.CloseConnection(); }
        }
        if (mid > 0)
        {
            objDT = BO_Module.Search(mid);
            if (objDT != null && objDT.Rows.Count > 0)
            {
                int type = Convert.ToInt32(objDT.Rows[0]["TYPE_ID"]);
                if (type != 1 && type != 2)
                    mc = Convert.ToInt32(objDT.Rows[0]["MODULE_CONTENT"]);
            }
        }
        else
            mc = Convert.ToInt32(ddlCategory.SelectedValue);
    }
    protected void loadData()
    {
        try
        {
            objDT = BO_Articles.SelectAdmin(keyword, mid, mc, base.Cur_page, base.Page_size);
            bindRepeater(rptNews, objDT, pagerContainer, divMessage, divResult);
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally { base.CloseConnection(); }
    }
    //protected void rptNews_ItemCommand(object source, RepeaterCommandEventArgs e)
    //{
    //    try
    //    {
    //        base.OpenConnection();
    //        Button btnID = (Button)e.Item.FindControl("btnID");
    //        int a_id = Convert.ToInt32(btnID.CommandArgument);
    //        string commandName = e.CommandName;
    //        if (commandName.Equals("del"))
    //        {
    //            BO_Articles.Delete(a_id);
    //            divMessage.InnerHtml = O2S_Message.Success("Thông Báo", "Xóa thành công .");
    //        }
    //        else if (commandName.Equals("Display"))
    //            BO_Articles.IsHot(a_id, m_id);
    //        loadData();
    //    }
    //    catch (Exception ex)
    //    {
    //        divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Phát hiện lỗi: <br />" + ex.Message);
    //    }
    //    finally { base.CloseConnection(); }
    //}
    //protected void rptNews_ItemDataBound(object sender, RepeaterItemEventArgs e)
    //{
    //    if ((m_c == -1 && type == null) || (!type.Equals("1") && !type.Equals("9")))
    //    {
    //        Button btnDisplay = (Button)e.Item.FindControl("btnDisplay");
    //        if (btnDisplay != null)
    //            if (!Convert.ToBoolean(btnDisplay.CommandArgument))
    //                btnDisplay.CssClass = "icon-32-cancel";
    //            else
    //                btnDisplay.CssClass = "icon-32-checking";
    //    }
    //}
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        base.search(txtSearch.Value.Trim());
    }
    protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        base.ddlSelectedIndexChanged("cid", ddlCategory);
    }
    protected void btnDel_Click(object sender, EventArgs e)
    {
        string del_list = hdfID.Value;
        string[] sp_list = del_list.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
        BOArticles_Delete _action = new BOArticles_Delete();
        usp_Articles_Delete _entity;
        List<usp_Articles_Delete> _listData = new List<usp_Articles_Delete>();
        foreach (string per in sp_list)
        {
            _entity = new usp_Articles_Delete();
            _entity.ARTICLE_ID = Convert.ToInt32(per);
            _listData.Add(_entity);
        }
        if (_action.ExecuteListDataAndReturnBooleanValue(_listData))
            divMessage.InnerHtml = O2S_Message.Success("Thông Báo", "Thực hiện thành công.");
        else
            divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Chưa thực hiện được !");
        loadData();
    }
}
